package com.study.zzedu.fragment.center;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.leon.lib.settingview.LSettingItem;
import com.study.zzedu.MyApplication;
import com.study.zzedu.R;
import com.study.zzedu.base.BaseFragment;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.ui.appointexam.AppointExamActivity;
import com.study.zzedu.ui.classchat.ClassChatActivity;
import com.study.zzedu.ui.graduateservice.GraduatesServiceActivity;
import com.study.zzedu.ui.login.LoginActivity;
import com.study.zzedu.ui.modifypwd.ModifyPwdActivity;
import com.study.zzedu.ui.schoolroll.SchoolRollActivity;
import com.study.zzedu.ui.tuitioninfo.TuitionInfoActivity;
import com.study.zzedu.ui.versioninfo.VersionInfoActivity;
import com.study.zzedu.ui.yidonginfo.YiDongInfoActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.SharedPreferencesHelper;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by kiros on 2020/9/3.
 */

public class StudyCenterFragment extends BaseFragment<StudyCenterContract.IView, StudyCenterPresenter> implements StudyCenterContract.IView, View.OnClickListener{

    private TextView tv_to_roll,txt_user_major,txt_user_grade,txt_user_name;
    private LSettingItem item_cost,item_class,item_change,item_stu_service;
    private LSettingItem item_test,item_changepwd,item_versioninfo;

    private Button btn_quit_account;
    @Override
    protected StudyCenterPresenter createPresenter() {
        return new StudyCenterPresenter();
    }

    @Override
    protected StudyCenterContract.IView createView() {
        return this;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_center;
    }

    @Override
    protected void initView(View view) {
        txt_user_name  = view.findViewById(R.id.txt_user_name);
        txt_user_grade  = view.findViewById(R.id.txt_user_grade);
        txt_user_major  = view.findViewById(R.id.txt_user_major);

        tv_to_roll = view.findViewById(R.id.txt_to_schoolroll);

        item_cost = view.findViewById(R.id.item_cost);
        item_class = view.findViewById(R.id.item_class);
        item_change = view.findViewById(R.id.item_change);

        item_stu_service = view.findViewById(R.id.item_stu_service);

        item_test = view.findViewById(R.id.item_test);
        item_changepwd = view.findViewById(R.id.item_changepwd);
        item_versioninfo = view.findViewById(R.id.item_versioninfo);
        btn_quit_account = view.findViewById(R.id.btn_quit_account);
    }

    @Override
    protected void initData() {
        getPresenter().getUserInfo(getActivity());
    }

    @Override
    protected void initListener() {
        tv_to_roll.setOnClickListener(this);
        btn_quit_account.setOnClickListener(this);
        item_cost.setmOnLSettingItemClick(new MyClick(R.id.item_cost));
        item_class.setmOnLSettingItemClick(new MyClick(R.id.item_class));
        item_change.setmOnLSettingItemClick(new MyClick(R.id.item_change));
        item_test.setmOnLSettingItemClick(new MyClick(R.id.item_test));
        item_changepwd.setmOnLSettingItemClick(new MyClick(R.id.item_changepwd));
        item_versioninfo.setmOnLSettingItemClick(new MyClick(R.id.item_versioninfo));
        item_stu_service.setmOnLSettingItemClick(new MyClick(R.id.item_stu_service));
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.txt_to_schoolroll: Intent intent = new Intent(MyApplication.getAppContext(), SchoolRollActivity.class);startActivity(intent); break;
            case R.id.btn_quit_account:
                getPresenter().logout(MyApplication.getAppContext());
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SharedPreferencesHelper.getInstance(getActivity()).put("loginState", AppConstant.NOT_AUTO_LOGIN);
                        startActivity(new Intent(getContext(), LoginActivity.class));
                        getActivity().finish();
                    }
                },300);
                break;
        }
    }

    @Override
    public void getData(String data) {
        try{
            JSONObject jObject = new JSONObject(data);
            JSONArray jsonArray = jObject.getJSONArray("data");
            if (jsonArray.length() > 0) {
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                txt_user_name.setText(jsonObject.getString("name"));
                txt_user_grade.setText(jsonObject.getString("grade"));
                txt_user_major.setText(jsonObject.getString("major"));
            }
        }catch (Exception e){
            e.toString();
        }
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void getLogoutBack(String name) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == AppConstant.result_code_back_to_home){
            startActivity(new Intent(getActivity(), LoginActivity.class));
            getActivity().finish();
        }
    }

    class MyClick implements LSettingItem.OnLSettingItemClick{

        public int theId;
        public MyClick(int id){
            this.theId = id;
        }
        @Override
        public void click() {
            switch (this.theId){
                case R.id.item_cost: Intent intent = new Intent(MyApplication.getAppContext(), TuitionInfoActivity.class);startActivity(intent); break;
                case R.id.item_class: Intent intent2 = new Intent(MyApplication.getAppContext(), ClassChatActivity.class);startActivity(intent2); break;
                case R.id.item_change: Intent intent3 = new Intent(MyApplication.getAppContext(), YiDongInfoActivity.class);startActivity(intent3); break;
                case R.id.item_test: Intent intent4 = new Intent(MyApplication.getAppContext(), AppointExamActivity.class);startActivity(intent4); break;
                case R.id.item_changepwd: Intent intent6 = new Intent(MyApplication.getAppContext(), ModifyPwdActivity.class);startActivityForResult(intent6,15164); break;
                case R.id.item_versioninfo: Intent intent7 = new Intent(MyApplication.getAppContext(), VersionInfoActivity.class);startActivity(intent7); break;

                case R.id.item_stu_service: Intent intent8 = new Intent(MyApplication.getAppContext(), GraduatesServiceActivity.class);startActivity(intent8); break;
            }
        }
    }

}
